<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户交互</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>公告列表</span>
      </div>
      <!-- 搜索区域 -->
      <el-input placeholder="请输入内容" v-model="queryInfo.searchContent" clearable
                @clear="getAnnouncementList">
        <el-button slot="append" icon="el-icon-search" @click="getAnnouncementList"></el-button>
      </el-input>
      <el-button class="addButton" type="primary" @click="showAddAnnouncementDialog">新增</el-button>
      <!-- 公告列表区域-->
      <el-table :data="announcementList" border stripe
                :header-cell-style="{'text-align':'center'}"
                :cell-style="{'text-align':'center'}">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="date" label="日期" width="200px"></el-table-column>
        <el-table-column prop="introduction" label="公告简介"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <!--查看详情按钮-->
            <el-tooltip class="item" effect="dark" content="查看详情" placement="top" :enterable="false">
              <el-button type="info" icon="el-icon-zoom-in" size="mini" @click="showCheckAnnouncementDialog(scope.row.id)"></el-button>
            </el-tooltip>
            <!--编辑公告按钮-->
            <el-tooltip class="item" effect="dark" content="修改公告" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditAnnouncementDialog(scope.row.id)"></el-button>
            </el-tooltip>
            <!--删除公告按钮-->
            <el-tooltip class="item" effect="dark" content="删除公告" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="showDeleteAnnouncementDialog(scope.row.id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="total"
        :current-page="queryInfo.nowpage"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-card>
    <!-- 添加公告的对话框 -->
    <el-dialog title="公告" :visible.sync="addAnnouncementDialog" width="40%" height="500px" @close="addAnnouncementDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="announcementForm" :rules="rules" ref="formRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="简介" prop="introduction">
          <el-input type="textarea"
                    placeholder="请输入公告简介"
                    class="form-input"
                    v-model="announcementForm.introduction"
                    :autosize="{ minRows: 2, maxRows: 10}"
                    maxlength="50"
                    show-word-limit
                    clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea"
                    placeholder="请输入公告内容"
                    class="form-input"
                    v-model="announcementForm.content"
                    :autosize="{ minRows: 4, maxRows: 10}"
                    maxlength="255"
                    show-word-limit
                    clearable>
          </el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addAnnouncementDialog = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit('formRef')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看公告的对话框 -->
    <el-dialog title="公告详情" :visible.sync="checkAnnouncementDialog" width="40%" height="500px">
      <!-- 内容主体区域 -->
      <el-form :model="announcementInfoForm" label-width="100px">
        <el-form-item label="简介" prop="introduction">
          <el-input type="textarea"
                    class="form-input"
                    v-model="announcementInfoForm.introduction"
                    :autosize="{ minRows: 2, maxRows: 10}"
                    disabled>
          </el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea"
                    class="form-input"
                    v-model="announcementInfoForm.content"
                    :autosize="{ minRows: 4, maxRows: 10}"
                    disabled>
          </el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 修改公告的对话框 -->
    <el-dialog title="修改公告" :visible.sync="editAnnouncementDialog" width="40%" @close="editAnnouncementDialogClosed">
      <!-- 内容主体 -->
      <el-form :model="editAnnouncementForm" ref="editAnnouncementFormRef" :rules="rules" label-width="100px">
        <el-form-item label="简介" prop="introduction">
          <el-input type="textarea"
                    placeholder="请输入公告简介"
                    class="form-input"
                    v-model="editAnnouncementForm.introduction"
                    :autosize="{ minRows: 2, maxRows: 10}"
                    maxlength="50"
                    show-word-limit
                    clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea"
                    placeholder="请输入公告内容"
                    class="form-input"
                    v-model="editAnnouncementForm.content"
                    :autosize="{ minRows: 4, maxRows: 10}"
                    maxlength="255"
                    show-word-limit
                    clearable>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAnnouncementDialog = false">取 消</el-button>
        <el-button type="primary" @click="editAnnouncement">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除公告的对话框 -->
    <el-dialog title="删除公告提示" :visible.sync="deleteAnnouncementDialog" width="30%" >
      <!-- 内容主体区域 -->
      <span>确定删除该公告吗?</span>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteAnnouncementDialog = false">取 消</el-button>
        <el-button type="primary" @click="deleteAnnouncement">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        //公告列表
        announcementList: [],
        // 总数
        total: 0,
        queryInfo: {
          //当前页数
          nowPage: 1,
          //当前每页显示多少条数据
          pageSize: 10,
          //搜索内容
          searchContent: '',
        },
        //操作的表格对象id
        idInfo: {
          announcementId: null
        },
        //新增公告对话框可见与否
        addAnnouncementDialog: false,
        //新增的公告表单
        announcementForm: {
          introduction: '',
          content: '',
        },
        //表单验证规则
        rules: {
          introduction: [
            { required: true, message: '请输入公告简介', trigger: 'blur' },
            { maxlength: 50, message: '最大长度为50', trigger: 'blur'}
          ],
          content: [
            { required: true, message: '请输入公告内容', trigger: 'blur' },
            { maxlength: 255, message: '最大长度为255', trigger: 'blur'}
          ]
        },
        //查看公告对话框可见与否
        checkAnnouncementDialog: false,
        //公告详情表单
        announcementInfoForm:{},
        //修改公告对话框可见与否
        editAnnouncementDialog: false,
        //修改公告的表单
        editAnnouncementForm: {},
        //删除公告对话框可见与否
        deleteAnnouncementDialog: false,
      }
    },
    created () {
      this.getAnnouncementList()
    },
    methods: {
      /**
       * 获取公告列表
       */
      async getAnnouncementList () {
        await this.$http.get('announcements', { params: this.queryInfo }).then(res=> {
          if (res.status !== 200) {
            if(res.status === 403) {
              return this.$message.error('您没有获取公告列表的权限!')
            }
            return this.$message.error('获取公告列表失败!')
          }
          //console.log(res)
          this.total = res.data.total
          this.announcementList = res.data.announcements
        })
      },

      /**
       * 监听分页尺寸改变
       * @param newSize:新的分页尺寸
       */
      handleSizeChange(newSize) {
        this.queryInfo.pageSize=newSize
        this.getAnnouncementList()
      },

      /**
       * 监听当前页码变化
       * @param nowPage:当前页码
       */
      handleCurrentChange(nowPage){
        this.queryInfo.nowPage=nowPage
        this.getAnnouncementList()
      },

      /**
       * 展示添加公告的对话框
       */
      showAddAnnouncementDialog() {
        this.addAnnouncementDialog=true;
      },

      /**
       * 提交公告
       * @param formName:公告表单引用名
       */
      formSubmit(formName){
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let res = await this.$http.post('announcement',this.announcementForm)
            if(res.status!==200){
              if(res.status === 403) {
                return this.$message.error('您没有发布公告的权限!')
              }
              return this.$message.error('发布公告失败,请稍后再试')
            }
            this.addAnnouncementDialog = false
            this.$message.success("发布公告成功")
            this.getAnnouncementList()
          } else {
            return this.$message.error('表单信息不合法!')
          }
        });
      },

      /**
       * 关闭添加公告对话框之后的重置
       */
      addAnnouncementDialogClosed(){
        this.$refs.formRef.resetFields();
      },

      /**
       * 展示查看公告的对话框
       * @param id 公告ID
       */
      async showCheckAnnouncementDialog(id){
        this.idInfo.announcementId=id
        //console.log(this.idInfo.announcementId)
        let res=await this.$http.get('announcement', { params: this.idInfo })
        if (res.status !== 200) {
          return this.$message.error('获取公告详情失败!')
        }
        this.announcementInfoForm=res.data
        this.checkAnnouncementDialog = true
      },

      /**
       * 展示修改公告的对话框
       */
      async showEditAnnouncementDialog (id) {
        this.idInfo.announcementId = id
        let res=await this.$http.get('announcement', { params: this.idInfo })
        if (res.status !== 200) {
          return this.$message.error('获取公告信息失败！')
        }
        this.editAnnouncementForm = res.data
        this.editAnnouncementDialog = true
      },

      /**
       * 修改公告
       */
      editAnnouncement () {
        // 提交请求前，表单预验证
        this.$refs.editAnnouncementFormRef.validate(async valid => {
          // 表单预校验失败
          if (!valid) {
            return this.$message.error('表单信息不合法!')
          }
          let res = await this.$http.put('announcement',this.editAnnouncementForm)
          if (res.status !== 200) {
            if(res.status === 403) {
              return this.$message.error('您没有修改公告的权限!')
            }
            return this.$message.error('网络开小差了，请稍后再试！')
          }
          console.log(res)
          // 隐藏添加用户对话框
          this.editAnnouncementDialog = false
          this.$message.success('修改公告成功！')
          this.getAnnouncementList()
        })
      },

      /**
       * 关闭修改公告对话框之后的重置
       */
      editAnnouncementDialogClosed(){
        this.$refs.editAnnouncementFormRef.resetFields();
      },

      /**
       * 展示删除公告的对话框
       */
      showDeleteAnnouncementDialog (id) {
        this.idInfo.announcementId = id
        this.deleteAnnouncementDialog = true
      },

      /**
       * 删除公告
       */
      async deleteAnnouncement() {
        let res = await this.$http.delete('announcement',{params: this.idInfo})
        if (res.status !== 200) {
          if(res.status === 403) {
            return this.$message.error('您没有删除公告的权限!')
          }
          return this.$message.error('删除公告失败!')
        } else {
          this.$message.success('删除成功')
        }
        this.getAnnouncementList()
        this.deleteAnnouncementDialog = false
      }
    }
  }
</script>
<style lang="less" scoped>
  .el-input {
    width: 500px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
  .addButton {
    margin-left: 20px;
  }
  .el-table {
    margin-top: 15px;
    font-size: 12px;
  }
  .el-pagination {
    margin-top: 15px;
  }
  .form-input {
    width: 90%;
  }
</style>
